//
//  SettingController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD
import SDWebImage

class SettingController: BaseViewController {
    
    var model : PersonalDataModel?
    
    var didmissMessage = ""
    

    lazy var titleAndImage : [[String : String]] = {
        
        return [["title" : "修改密码","imageName" : "settingImage1"],["title" : "设置支付密码","imageName" : "settingImage2"],["title" : "个人资料","imageName" : "settingImage3"],["title" : "收货地址","imageName" : "settingImage4"],["title" : "商城","imageName" : "settingImage"],["title" : "关于我们","imageName" : "settingImage6"],["title" : "清除缓存","imageName" : "settingImage7"],["title" : "帮助中心","imageName" : "settingImage8"]]
        
        
    }()
    
    
    
    lazy var titleAndImage1 : [[String : String]] = {
        
        return [["title" : "修改密码","imageName" : "settingImage1"],["title" : "设置支付密码","imageName" : "settingImage2"],["title" : "个人资料","imageName" : "settingImage3"],["title" : "收货地址","imageName" : "settingImage4"],["title" : "商城","imageName" : "settingImage"],["title" : "关于我们","imageName" : "settingImage6"],["title" : "清除缓存","imageName" : "settingImage7"],["title" : "帮助中心","imageName" : "settingImage8"],["title" : "社区加入","imageName" : "settingImage8"]]
        
        
    }()
    
    
    lazy var logLabel : UILabel = {
    
    let temp = UILabel()
        
        temp.font = UIFont.systemFont(ofSize: 16)
        
        temp.textColor = UIColor.dakGray()
        
        let str = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String
        
        temp.text = "小鲸 " + str!
        
        temp.sizeToFit()
        
        return temp
    }()
    
    
    let loginBtn : UIButton = {
    
    
        let btn = UIButton(type: .custom)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 19)
        
        btn.setTitleColor(UIColor.white, for: .normal)
        
        btn.frame = CGRect(x: 20, y: 50, width: KScreenW - 40, height: 46)
        
        btn.backgroundColor = UIColor.themeColor()
        
        btn.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 5)
        
      
        btn.addTarget(self, action: #selector(logInToOut(btn:)), for: .touchUpInside)

    
        return btn
    
    }()
    
    
    lazy var footerView : UIView = {
    
        let footView = UIView(frame: CGRect(x: 0, y: 0, width: KScreenW, height: 100))
        
        footView.backgroundColor = UIColor.viewBgGrey()
        
        
        footView.addSubview(self.loginBtn)

        self.logLabel.center.x = self.loginBtn.center.x
        
        self.logLabel.center.y = self.loginBtn.frame.origin.y - 6 - self.logLabel.frame.size.height

        footView.addSubview(self.logLabel)
    
        return footView
        
    }()
    
    
    
    func logInToOut(btn: UIButton){
        
        if UserViewModel.shared.userIsLogin() == true {
            
            clearData()
            
            loginBtn.setTitle("登 录", for: .normal)

        } else {
            
            login()
            

        }
        
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
        if UserViewModel.shared.userIsLogin() == true {
            
            loginBtn.setTitle("退出登录", for: .normal)
            
        }else{
            
            loginBtn.setTitle("登 录", for: .normal)
            
            
        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "设置"
        
        plainTableView.backgroundColor = UIColor.viewBgGrey()
        
        plainTableView.tableFooterView = footerView
        
        plainTableView.register(BaseTableViewCell.self, forCellReuseIdentifier: BaseTableViewCell.identifer())
        
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: SettingCommonCell.self), bundle: nil), forCellReuseIdentifier: SettingCommonCell.identifer())

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    



}




// MARK:- tableview代理方法
extension SettingController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if HomeGloabeModel.golableModel.ios_tag == "0" {
            return 8
            
        } else {
            
            return 9
            
        }
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        switch indexPath.row {
        case 4:
            
            let tempCell : BaseTableViewCell =  tableView.dequeueReusableCell(withIdentifier: BaseTableViewCell.identifer()) as! BaseTableViewCell
            tableCell = tempCell
            
        default:
            let tempCell : SettingCommonCell =  tableView.dequeueReusableCell(withIdentifier: SettingCommonCell.identifer()) as! SettingCommonCell
            
            
            if HomeGloabeModel.golableModel.ios_tag == "0" {
                
                tempCell.dic = titleAndImage[indexPath.row]
                
            } else {
                
                tempCell.dic = titleAndImage1[indexPath.row]
                
            }

            
            
            tableCell = tempCell
            
            
        }
        
        
        return tableCell!
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.row {
        case 4:
            return 10
        default:
            return 60
            
        }
        
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        if indexPath.row == 0 {
            
            if UserViewModel.shared.userIsLogin() == false {
                
                login()
                
                return
            }
            
            let temp = UpLoadIconController()
            
            temp.urlStr = self.model!.modify_pwd_url
            
            navigationController?.pushViewController(temp, animated: true)
            
        }else if indexPath.row == 1 {
            
            if UserViewModel.shared.userIsLogin() == false {
                
                login()
                
                return
            }
            
            let temp = UpLoadIconController()
            
            temp.urlStr = self.model!.set_paypwd_url
            
            navigationController?.pushViewController(temp, animated: true)
            
        }else if indexPath.row == 2 {
            
            if UserViewModel.shared.userIsLogin() == false {
                
                login()
                
                return
            }
            
            navigationController?.pushViewController(PersonalInfoVC(), animated: true)
            
        }else if indexPath.row == 3 {
            
            if UserViewModel.shared.userIsLogin() == false {
                
                login()
                
                return
            }
            
            navigationController?.pushViewController(AdressListController(), animated: true)
        }else if indexPath.row == 5 {
            
            let temp = UpLoadIconController()
            
            temp.urlStr = HomeGloabeModel.golableModel.aboutus_url
            
            navigationController?.pushViewController(temp, animated: true)
            
        }else if indexPath.row == 6 {
            
          SVProgressHUD.show(withStatus: "正在清除缓存...")
            
           didmissMessage = "成功清楚\(fileSizeWithInterge())缓存"
            

           SDImageCache.shared().clearDisk(onCompletion: {
                    QL1(Thread.current)

            SVProgressHUD.showSuccess(withStatus: self.didmissMessage)
            
            self.plainTableView.reloadData()
                        
                })
        
      
            
//          SDImageCache.shard.get
            
            
        }else if indexPath.row == 7 {
            
            let temp = UpLoadIconController()
            
            temp.urlStr = HomeGloabeModel.golableModel.help_url
            
            navigationController?.pushViewController(temp, animated: true)
            
        }else if indexPath.row == 8 {
            
            
            if UserViewModel.shared.userIsLogin() == false {
                
                login()
                
                return
            }

            
            let temp = BiotopeJionVC()
            
            
            navigationController?.pushViewController(temp, animated: true)
        }

    }
    
    
    func fileSizeWithInterge() -> String {
        
        
        let temp = SDImageCache.shared().getSize()
        
        if (temp < 1024) {// 小于1k
            return "\(temp)B"
        }else if (temp < 1024 * 1024){// 小于1m
            let aFloat : Float = Float(temp)/1024;
            return String(format: "%.2fKB", arguments: [aFloat])
        }else if (temp < 1024 * 1024 * 1024){// 小于1G
            let aFloat : Float = Float(temp)/(1024 * 1024)
            
            return String(format: "%.2fMB", arguments: [aFloat]) //"\(aFloat)MB"
        }else{
            let aFloat : Float = Float(temp)/(1024 * 1024 * 1024)
            return String(format: "%.2fGB", arguments: [aFloat]) //"\(aFloat)GB"
        }
        
    }
    

    
    
    
}
